package com.controller;

import java.io.File;


import java.io.IOException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.Course;
import com.server.CourseServer;
import com.server.RecordServer;
import com.server.SysuserServer;
import com.util.PageBean;

@Controller
public class CourseController {
	@Resource
	private CourseServer courseService;
	@Resource
	private SysuserServer userService;
	@Resource
	private RecordServer recordService;

	// 文件上传
	public String fileUpload(@RequestParam(value = "file", required = false) MultipartFile file,
			HttpServletRequest request, String img) {
		String path = request.getSession().getServletContext().getRealPath("upload");
		String fileName = file.getOriginalFilename();
		File targetFile = new File(path, fileName);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		try {
			file.transferTo(targetFile);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String pa = request.getContextPath() + "/upload/" + fileName;
		System.out.println("path===" + pa);
		if (fileName != null && !fileName.equals("")) {
			img = fileName;
		} else {
			img = "zanwu.jpg";
		}
		return img;

	}

	@RequestMapping("admin/addCourse.do")
	public String addCourse(HttpServletRequest request, Course Course, HttpSession session) {
		System.out.println("ddddd");
			java.sql.Timestamp time = new Timestamp(System.currentTimeMillis());
			Course.setPubtime(time.toString().substring(0, 19));
			courseService.add(Course);
			return "redirect:courseList.do";
	}
// 处理更新课程
	@RequestMapping("admin/doUpdateCourse.do")
	public String doUpdateCourse(ModelMap map, int id) {
		map.put("jz", courseService.getById(id));
		Map<String, Object> umap = new HashMap<>();
		umap.put("utype", "培训师");
		umap.put("status", "空闲中");
		map.put("slist", userService.getAll(umap));
		map.put("ulist", userService.getAll(null));
		return "admin/course_update";
	}
	
	//查看组信息
	@RequestMapping("admin/course_detail.do")
	public String course_detail(ModelMap map, int id) {
		map.put("jz", courseService.getById(id));
		return "admin/course_detail";
	}
// 保存课程信息
	@RequestMapping("admin/updateCourse.do")
	public String updateCourse(HttpServletRequest request, Course course) {
		courseService.update(course);
		return "redirect:courseList.do";
	}

	// 选题详情
	@RequestMapping("admin/showCoursex.do")
	public String showCourse(ModelMap map, int id) {
		map.put("jz", courseService.getById(id));
		return "admin/coursex";
	}

	// 分页查询
	@RequestMapping("admin/courseList.do")
	public String jianList(@RequestParam(value = "page", required = false) String page, ModelMap map,
			HttpSession session) {

		if (page == null || page.equals("")) {
			page = "1";
		}
		PageBean pageBean = new PageBean(Integer.parseInt(page), PageBean.PAGESIZE);
		Map<String, Object> pmap = new HashMap<String, Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", pageBean.getPageSize());
		Map<String, Object> cmap = new HashMap<String, Object>();
		int total = courseService.getCount(cmap);
		pageBean.setTotal(total);
		List<Course> list = courseService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 1);
		map.put("ulist", userService.getAll(null));
		Map<String, Object> umap = new HashMap<>();
		umap.put("utype", "培训师");
		umap.put("status", "空闲中");
		map.put("slist", userService.getAll(umap));
		return "admin/course_list";
	}

	// 分页模糊查询
	@RequestMapping("admin/vagueCourseList.do")
	public String vagueCourseList(@RequestParam(value = "page", required = false) String page, ModelMap map,
			HttpSession session, Course cd) {
		if (page == null || page.equals("")) {
			page = "1";
		}
		PageBean pageBean = new PageBean(Integer.parseInt(page), PageBean.PAGESIZE);
		Map<String, Object> pmap = new HashMap<String, Object>();
		pmap.put("pageno", pageBean.getStart());
		pmap.put("pageSize", pageBean.getPageSize());

		Map<String, Object> cmap = new HashMap<String, Object>();
		if (cd.getName() != null && !cd.getName().equals("")) {
			cmap.put("name", cd.getName());
			pmap.put("name", cd.getName());
		}

		int total = courseService.getCount(cmap);
		pageBean.setTotal(total);
		List<Course> list = courseService.getByPage(pmap);
		map.put("page", pageBean);
		map.put("list", list);
		session.setAttribute("p", 2);
		Map<String, Object> umap = new HashMap<>();
		umap.put("utype", "培训师");
		umap.put("status", "空闲中");
		map.put("slist", userService.getAll(umap));
		return "admin/course_list";
	}

	@RequestMapping("admin/deleteCourse.do")
	public String deleteCourse(int id,HttpSession session) {
		/*Map<String, Object> rmap = new HashMap<>();
		int count = 0;
		rmap.put("gid", id);
		count = userService.getCount(rmap);
		if (count > 0) {
			session.setAttribute("info", "组下有人，不可删除！");
			return "admin/error";
		} else {*/
			courseService.delete(id);
			return "redirect:courseList.do";
		/*}*/

	}
}
